package cn.zhizihua.service.impl;

import cn.zhizihua.domain.po.Cart;
import cn.zhizihua.mapper.CartMapper;
import cn.zhizihua.service.ICartService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhizihua
 * @since 2024-12-11
 */
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements ICartService {

    @Autowired
    CartMapper cartMapper;
    @Override
    public boolean cartSaveOrUpdate(Cart cart) {
        //标记 操作是否成功
        int i = 0;
        //判断新增Or修改
        if (cart.getCartId()!=null && cart.getCartId()!=0){
            //修改
            i = cartMapper.updateById(cart);
            System.out.println("up1111111");
        }else {
            //新增购物车项，先判断购物车项是否存在
            LambdaQueryWrapper<Cart> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Cart::getFoodId,cart.getFoodId())
                    .eq(Cart::getBusinessId,cart.getBusinessId())
                    .eq(Cart::getUserId,cart.getUserId());
            //搜索到的购物车项
            Cart one = getOne(queryWrapper);
            if (one!=null){
                //存在，更新数量
                // 得到的购物车项数量重新赋值
                one.setQuantity(one.getQuantity()+cart.getQuantity());
                System.out.println("up222222"+"-----"+one.getCartId());
                // 更新数量
                i = cartMapper.updateById(one);
            }else {
                //不存在，直接新增
                i = cartMapper.insert(cart);
            }
        }
        if (i>0){
            //操作成功
            return true;
        }
        //操作失败
        return false;
    }
}
